package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.easyframe.action.permission.SystemMessageModel;
import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.SystemMessageDAO;
import cn.org.timespace.easyframe.model.permission.EfSystemHelp;
import cn.org.timespace.easyframe.model.permission.SystemMessage;
import cn.org.timespace.easyframe.util.datatime.DateTimeUtils;

public class SystemMessageDAOImpl extends EfBaseDAO implements SystemMessageDAO {
	
	public void save(SystemMessage transientInstance) {
		log.debug("saving SystemMessage instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(SystemMessage instance) {
		getHibernateTemplate().update(instance);
	} 

	public void delete(SystemMessage persistentInstance) {
		log.debug("deleting SystemMessage instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SystemMessage findById(java.lang.String id) {
		log.debug("getting SystemMessage instance with id: " + id);
		try {
			SystemMessage instance = (SystemMessage) getHibernateTemplate().get(
					"cn.org.timespace.easyframe.model.permission.SystemMessage", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<SystemMessage> findByCondition(SystemMessageModel model){
		log.debug("finding all SystemMessage instances");
		
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from SystemMessage a where 1=1 ");
			
			if(!StringUtils.isEmpty(model.getTitle())){
				queryString.append(" and a.title like '%"+model.getTitle().trim()+"%' ");
			}
			
			if(!StringUtils.isEmpty(model.getDicMsgType())){
				queryString.append(" and a.dicMsgType = '"+model.getDicMsgType()+"'");
			}
			
			if(!StringUtils.isEmpty(model.getOperatorDateStart()) && !StringUtils.isEmpty(model.getOperatorDateEnd())){
				queryString.append(" and a.operatorDate >= :operatorDateStart ");
				queryString.append(" and a.operatorDate <= :operatorDateEnd ");
			}else if(!StringUtils.isEmpty(model.getOperatorDateStart())){
				queryString.append(" and a.operatorDate >= :operatorDateStart ");
			}else if(!StringUtils.isEmpty(model.getOperatorDateEnd())){
				queryString.append(" and a.operatorDate <= :operatorDateEnd ");
			}
			
			queryString.append(" order by operatorDate desc ");
			
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			
			if(!StringUtils.isEmpty(model.getOperatorDateStart()) && !StringUtils.isEmpty(model.getOperatorDateEnd())){
				query.setDate("operatorDateStart", java.sql.Date.valueOf(model.getOperatorDateStart()));
				query.setDate("operatorDateEnd", java.sql.Date.valueOf(model.getOperatorDateEnd()));
			}else if(!StringUtils.isEmpty(model.getOperatorDateStart())){
				query.setDate("operatorDateStart", java.sql.Date.valueOf(model.getOperatorDateStart()));
			}else if(!StringUtils.isEmpty(model.getOperatorDateEnd())){
				query.setDate("operatorDateEnd", java.sql.Date.valueOf(model.getOperatorDateEnd()));
			}			
			
			return query.list();
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			if(session != null) session.close();
			if(query != null) query = null;
		}
	}
	
	public List<SystemMessage> findValidSystemMessage() {
		log.debug("finding all SystemMessage instances");
		
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from SystemMessage a where 1=1 ");
			
			queryString.append(" and a.startDate <= :startDate ");
			queryString.append(" and a.endDate >= :endDate ");
			
			queryString.append(" order by operatorDate desc ");
			
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			
			query.setDate("startDate", java.sql.Date.valueOf(DateTimeUtils.getDefaultCurDate()));
			query.setDate("endDate", java.sql.Date.valueOf(DateTimeUtils.getDefaultCurDate()));
			
			return query.list();
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			if(session != null) session.close();
			if(query != null) query = null;
		}
	}
	
	public EfSystemHelp findSysteHelp(){
		StringBuffer queryString = new StringBuffer("from EfSystemHelp");
		List resList = getHibernateTemplate().find(queryString.toString());
		if(resList != null && resList.size() > 0){
			return (EfSystemHelp)resList.get(0);
		}
		return null;
	}
	
	public void updateEfSystemHelp(EfSystemHelp efSystemHelp){
		getHibernateTemplate().update(efSystemHelp);
	}	
}
